Microsoft Graph API is a unified API endpoint that allows developers to access a wide range of Microsoft 365 services, including Azure Active Directory, SharePoint, OneDrive, Microsoft Teams, and more. It serves as a single endpoint for interacting with various Microsoft cloud services. Below, I'll provide a brief overview of Microsoft Graph API and a simple example in Python using the requests library.
Users and Groups:
Mail and Calendar:
Drive and Files:
Security and Compliance:
Below is a simplified example demonstrating how to use the Microsoft Graph API to retrieve information about the signed-in user. Ensure you have the necessary Azure AD authentication details and replace placeholders with your actual values.
import requests
# Specify your Microsoft Graph API details
graph_api_endpoint = 'https://graph.microsoft.com/v1.0/me' # Endpoint to retrieve user's information
# Azure AD authentication details
tenant_id = 'your-tenant-id'
client_id = 'your-client-id'
client_secret = 'your-client-secret'
resource_url = 'https://graph.microsoft.com'
# Get Azure AD token for authentication
token_endpoint = f'https://login.microsoftonline.com/{tenant_id}/oauth2/token'
token_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'resource': resource_url
}
token_response = requests.post(token_endpoint, data=token_data)
access_token = token_response.json()['access_token']
# Retrieve user information from Microsoft Graph
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(graph_api_endpoint, headers=headers)
user_info = response.json()
# Print user details
print(f"User ID: {user_info['id']}")
print(f"User Name: {user_info['displayName']}")
print(f"User Email: {user_info['mail']}")
This example demonstrates how to retrieve information about the signed-in user from Microsoft Graph using the requests library in Python. Ensure that you replace the placeholder values with your actual Azure AD authentication information.
For production environments, it's recommended to use official SDKs provided by Microsoft, such as the msal library for authentication. Install the required library using:
bashpip install msal
Refer to the official Microsoft Graph API documentation for the latest information, API details, and examples.